/*
 * Sensor.java
 *
 * Created on 13 de Fevereiro de 2008, 00:42
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.ufal.ic.mobile.behaviours;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;

/**
 *
 * @author Raphael
 */
public class Sensor extends CyclicBehaviour {
    private String nameAgent;
    private String addressAgent;
    
    /** Creates a new instance of Sensor */
    public Sensor(String name, String address) {
        this.nameAgent = name;
        this.addressAgent = address;
    }

    public void action() {
        ACLMessage receiveMessage = myAgent.receive();
        AID receiver = new AID(this.nameAgent, true);
        receiver.addAddresses(this.addressAgent);
        
        if (receiveMessage != null) {
            ACLMessage sendMessage = new ACLMessage(ACLMessage.INFORM); 
            
            if (receiveMessage.getContent().startsWith("[SEH]")) {
                sendMessage.setContent(receiveMessage.getContent()+"#"+this.nameAgent+"#"+this.addressAgent);
                sendMessage.addReceiver(receiver);
                myAgent.send(sendMessage);             
            } else {
                sendMessage.setContent(receiveMessage.getContent());
                System.out.println("Agente "+myAgent.getLocalName()+" envia problema para "+receiver.getLocalName());                
            }              
            
            sendMessage.addReceiver(receiver);
            myAgent.send(sendMessage);    
            System.out.println("Agente terminou!");                           
            myAgent.doDelete();
        }
    }
    
}
